package cn.cloudplug.friendsrecommend;

import java.util.ArrayList;
import java.util.List;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.cloudplug.friendsrecommend.R;
import cn.cloudplug.friendsrecommend.entity.Team;
import cn.cloudplug.friendsrecommend.util.Dp2px;

public class MyTeamActivity extends Activity{
	private SwipeMenuListView lv_my_team;
	private MyTeamAdapter adapter;
	private List<Team> teams;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_team);
		getTestData();
		setView();
	}
	private void setView() {
		ViewGroup vp=(ViewGroup) findViewById(R.id.title);
		lv_my_team=(SwipeMenuListView) findViewById(R.id.lv_my_team);
		vp.findViewById(R.id.iv_home).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showHome();
			}
		});
		vp.findViewById(R.id.iv_more).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				addTeam();
			}
		});
		
		
		adapter=new MyTeamAdapter();
		
		
		SwipeMenuCreator creator = new SwipeMenuCreator() {
			@Override
			public void create(SwipeMenu menu) {
				/*添加文本item
				  //create "open" item Text
				  SwipeMenuItem openItem = new
				  SwipeMenuItem( getApplicationContext()); 
				  // set item  background 
				  openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE))); 
				  // set item  width 
				  openItem.setWidth(Dp2px.getPx(100, MyTeamActivity.this)); 
				  // set item title
				  openItem.setTitle("Open"); 
				  // set item title fontsize
				  openItem.setTitleSize(18); 
				  // set item title font color
				  openItem.setTitleColor(Color.WHITE); 
				  // add to menu
				  menu.addMenuItem(openItem);
				添加图片item		
				// create "delete" item Image
				SwipeMenuItem deleteItem = new SwipeMenuItem(
						MyTeamActivity.this);
				// set item background
				//deleteItem.setBackground(new ColorDrawable(Color.rgb(0xC9,
				//		0xC9, 0xCE)));
				// set item width
				deleteItem.setWidth(Dp2px.getPx(100, MyTeamActivity.this));
				// set a icon
				deleteItem.setIcon(R.drawable.cloud_trash_icon);
				// add to menu
				menu.addMenuItem(deleteItem);
		    */
				SwipeMenuItem editItem = new SwipeMenuItem(
						MyTeamActivity.this);
				// set item background
				editItem.setBackground(new ColorDrawable(Color.rgb(243,243, 243)));
				// set item width
				editItem.setWidth(Dp2px.getPx(80, MyTeamActivity.this));
				// set a icon
				editItem.setIcon(R.drawable.cloud_edit_pencil_icon);
				// add to menu
				menu.addMenuItem(editItem);
				
				SwipeMenuItem deleteItem = new SwipeMenuItem(
						MyTeamActivity.this);
				// set item background
				deleteItem.setBackground(new ColorDrawable(Color.rgb(243,243, 243)));
				// set item width
				deleteItem.setWidth(Dp2px.getPx(80, MyTeamActivity.this));
				// set a icon
				deleteItem.setIcon(R.drawable.cloud_trash_icon);
				// add to menu
				menu.addMenuItem(deleteItem);
			}
		};
		// set creator
		lv_my_team.setMenuCreator(creator);
		lv_my_team.setAdapter(adapter);
		
		lv_my_team.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(int position, SwipeMenu menu,int index) {
				switch (index) {
				case 0:
					Intent intent=new Intent(MyTeamActivity.this, MyTeamInfoActivity.class);
					intent.putExtra("team", teams.get(position));
					startActivity(intent);
					break;
				case 1:
					teams.remove(position);
					adapter.notifyDataSetChanged();
					break;
				}
				return false;
			}
		});
		lv_my_team.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
			}
		});
	}
	protected void showHome() {
		startActivity(new Intent(MyTeamActivity.this,MainActivity.class));
		
	}
	private void getTestData() {
		teams=new ArrayList<Team>();
		for(int i=0;i<3;i++){
			Team t=new Team(i, "我的团队"+i, "我的团队我最牛", "5015.05.05", i+4, i, i);
			teams.add(t);
		}
	}
	
	private void addTeam() {
		startActivityForResult(new Intent(this, AddTeamActivity.class), 0);
	}
	
	class MyTeamAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return teams.size();
		}

		@Override
		public Object getItem(int position) {
			return teams.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder vh=null;
			if(convertView==null){
				convertView=LayoutInflater.from(MyTeamActivity.this).inflate(R.layout.list_item_my_team, null);
				vh=new ViewHolder();
				vh.tv_teamName=(TextView) convertView.findViewById(R.id.tv_team_name);
				vh.tv_team_leader=(TextView) convertView.findViewById(R.id.tv_team_leader);
				vh.iv_operation=(ImageView) convertView.findViewById(R.id.iv_operation);
				vh.tv_mumberCount=(TextView) convertView.findViewById(R.id.tv_member_count);
				vh.tv_cupCount=(TextView) convertView.findViewById(R.id.tv_cup_count);
				convertView.setTag(vh);
			}
			vh=(ViewHolder) convertView.getTag();
			final Team t=teams.get(position);
			vh.tv_teamName.setText(t.getTeamName().toString());
			vh.tv_mumberCount.setText(t.getTeamMemberCount()+"人");
			vh.tv_cupCount.setText(t.getTeamCupCount()+"个");
			vh.iv_operation.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent=new Intent(MyTeamActivity.this, MyTeamInfoActivity.class);
					intent.putExtra("team", teams.get(position));
					startActivity(intent);
				}
			});
			return convertView;
		}
		class ViewHolder{
			TextView tv_teamName;
			TextView tv_mumberCount;
			TextView tv_cupCount;
			TextView tv_team_leader;
			ImageView iv_operation;
		}
	}
}
